home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / man / lib.fmt / c / mmap.man < prev    next >
Encoding:
Text File  |  1991-04-29  |  13.2 KB  |  331 lines

  1.  
  2.  
  3.  
  4. MMAP                  C Library Procedures                   MMAP
  5.  
  6.  
  7.  
  8. NNAAMMEE
  9.      mmap - map an open file into the process's address space
  10.  
  11. SSYYNNOOPPSSIISS
  12.      NNOOTTEE:: tthhiiss mmaann ppaaggee iiss iinnaaccccuurraattee..  IItt wwaass bboorrrroowweedd ffrroomm aannootthheerr mmaacchhiinnee
  13.      aanndd hhaassnn''tt bbeeeenn uuppddaatteedd ttoo mmaakkee iitt ccoorrrreecctt..
  14.      ##iinncclluuddee <<ssyyss//ttyyppeess..hh>>
  15.      ##iinncclluuddee <<ssyyss//mmmmaann..hh>>
  16.  
  17.      mmmmaapp((aaddddrr,, lleenn,, pprroott,, sshhaarree,, ffdd,, ppooss))
  18.      ccaaddddrr__tt aaddddrr;;   //** ssttaarrttiinngg vviirrtt--aaddddrr **//
  19.      iinntt     lleenn;;    //** lleennggtthh ((bbyytteess)) ttoo mmaapp **//
  20.      iinntt     pprroott;;   //** RROO,, RRWW eennccooddiinngg **//
  21.      iinntt     sshhaarree;;  //** pprriivvaattee//sshhaarreedd mmooddiiffiiccaattiioonnss **//
  22.      iinntt     ffdd;;     //** ooppeenn ffiillee ttoo bbee mmaappppeedd **//
  23.      ooffff__tt   ppooss;;    //** wwhheerree iinn ffiillee ttoo bbeeggiinn mmaapp **//
  24.  
  25. DDEESSCCRRIIPPTTIIOONN
  26.      _M_m_a_p causes the file referenced by _f_d, starting at byte _p_o_s
  27.      for _l_e_n bytes in the file, to be mapped into the calling
  28.      process's address space, starting at virtual address _a_d_d_r
  29.      for _l_e_n bytes, using protection specified by _p_r_o_t; modifica-
  30.      tions to the mapped memory are either private to the process
  31.      or shared, as specified by _s_h_a_r_e.  _M_m_a_p can be used to allo-
  32.      cate regions of shared memory, to map files into memory, and
  33.      to access special regions of the physical address space.
  34.  
  35.      _F_d must reference an open regular (IFREG) or character spe-
  36.      cial (IFCHR) file.  The device driver that implements the
  37.      IFCHR special file must support mapping for this to succeed.
  38.      Typically, a regular file is used to map shared memory.
  39.  
  40.      The _s_h_a_r_e argument specifies whether modifications to a
  41.      mapped file are to be kept private to the calling process or
  42.      shared with other processes accessing the file.  If _s_h_a_r_e is
  43.      MAP_SHARED, all modifications to the file are shared with
  44.      others who have it concurrently mapped.  If _s_h_a_r_e is
  45.      MAP_PRIVATE, all modifications are local to the calling pro-
  46.      cess; this doesn't restrict other processes from mapping the
  47.      file.  MAP_SHARED and MAP_PRIVATE have no relation to
  48.      _f_l_o_c_k(2), and do not restrict _r_e_a_d and _w_r_i_t_e system-calls.
  49.      If _s_h_a_r_e is MAP_ZEROFILL, the space indicated from _a_d_d_r for
  50.      _l_e_n bytes is replaced by private pages that are zero-filled
  51.      when referenced.  MAP_ZEROFILL ignores the _f_d argument, and
  52.      _p_o_s is ignored other than being checked for alignment
  53.      (specifying fd and pos = 0 is recommended).
  54.  
  55.      The _p_r_o_t argument should be PROT_WRITE for write access to
  56.      the mapped region, PROT_READ for read access, PROT_EXEC for
  57.      executable access.  These values can be ORed to obtain
  58.      read-write access, etc.  For programming convenience,
  59.      PROT_RDWR is defined as (PROT_READ|PROT_WRITE).  The file
  60.  
  61.  
  62.  
  63. DYNIX                                                           1
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. MMAP                  C Library Procedures                   MMAP
  71.  
  72.  
  73.  
  74.      access permissions on _f_d must allow the requested access.
  75.      The _p_r_o_t argument affects only the calling process; other
  76.      processes mapping the same file may have different access.
  77.  
  78.      The _a_d_d_r, _l_e_n, and _p_o_s arguments must be integral multiples
  79.      of the system page size, as defined by _g_e_t_p_a_g_e_s_i_z_e(2).  It
  80.      is possible to map over previously mapped pages.  If _a_d_d_r
  81.      and _l_e_n specify a nonexistent part of the process's address
  82.      space, the process's data segment is grown to accommodate
  83.      the request, and the process ``break'' (see _b_r_k(2)) is set
  84.      to the high end of the mapped region.  Reference to any
  85.      ``holes'' between the mapped region and the rest of the data
  86.      segment result in a segmentation fault (SIGSEGV).  _m_m_a_p does
  87.      not allow mapping over text or stack pages.
  88.  
  89.      When memory is mapped to a regular file, the file acts like
  90.      a paging area for the mapped memory region.  _R_e_a_d and _w_r_i_t_e
  91.      operations to mapped regions of the file also affect the
  92.      corresponding memory.  The memory contents are copied out to
  93.      the file when the process is swapped or when it exits, or
  94.      when the region is otherwise unmapped by the last process
  95.      that has it mapped.  For programs that use shared memory but
  96.      do not need a permanent disk image of the memory, the file
  97.      associated with _f_d can be _u_n_l_i_n_ked (see _u_n_l_i_n_k(2)) even
  98.      before the call to _m_m_a_p: if the file is _u_n_l_i_n_ked when the
  99.      region is unmapped, the disk space will not be updated.
  100.  
  101.      Regular files have their size rounded up to a file-system
  102.      block boundary.  Any non-existent space in the file at the
  103.      time of the _m_m_a_p request (for example, in a sparse file) is
  104.      allocated, and filled with zeroes when referenced.  Both of
  105.      these operations require write access to the file.
  106.  
  107.      The type of file referenced may impose further restrictions
  108.      on the _p_o_s, _o_f_f_s_e_t, or other parameters.  Refer to the
  109.      manual entry of the relevant device driver (for example,
  110.      _p_m_a_p(4)) for details.
  111.  
  112.      Closing a file descriptor previously used in an _m_m_a_p opera-
  113.      tion unmaps all pages mapped by that file descriptor (see
  114.      also _m_u_n_m_a_p(2)).  (Note: this may change in order to provide
  115.      compatibility with sunOS.) If the file-descriptor has been
  116.      _d_u_ped prior to being closed, no unmap takes place.
  117.  
  118.      _M_m_a_p can be called multiple times with the same file
  119.      descriptor, resulting in several (possibly overlapping)
  120.      mapped regions.  A process can have up to 8 regions mapped
  121.      simultaneously; mappings that are completely overlapped by
  122.      subsequent mappings are not counted in this total.  Mappings
  123.      which use the same file descriptor, and addr and pos argu-
  124.      ments which align virtually with a previous mapping, also
  125.      don't count in this total; the simplest case is mapping more
  126.  
  127.  
  128.  
  129. DYNIX                                                           2
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. MMAP                  C Library Procedures                   MMAP
  137.  
  138.  
  139.  
  140.      of a file, starting from the end of a previous mapping.
  141.  
  142.      All mapped files remain mapped in both the parent and child
  143.      process after a _f_o_r_k.  All flavors of the _e_x_e_c and _e_x_i_t sys-
  144.      tem calls, when successful, remove all maps the calling pro-
  145.      cess had established.  If a process has any maps, _v_f_o_r_k
  146.      behaves exactly like _f_o_r_k.  A child of a _v_f_o_r_k that has not
  147.      yet _e_x_e_ced a new image cannot successfully execute _m_m_a_p.
  148.  
  149.      There are three types of mapping: paged, physical, and non-
  150.      paged memory.  The type of mapping is determined by the type
  151.      of file being mapped.  Paged maps support shared memories
  152.      and mapped regular files.  Physical maps deal with hardware
  153.      that has restrictive access capability (for example, the
  154.      MULTIBUS address space, including Atomic Lock Memory).
  155.      Non-paged memory maps are typically used for special
  156.      reserved areas of system memory; they are assumed to behave
  157.      exactly like memory, supporting accesses of arbitrary size
  158.      and alignment, DMA, etc.
  159.  
  160.      System services (raw IO, read/write, stat, etc.) are sup-
  161.      ported in paged and non-paged memory maps; attempts at such
  162.      services in physically mapped address space result in an
  163.      error, typically EFAULT.  Core dumps include a copy of any
  164.      mapped address space; however, physically mapped addresses
  165.      read as zero.
  166.  
  167.      Regular files (IFREG) are always page-mapped.  Character
  168.      special files (IFCHR) can support paged, physical, or non-
  169.      paged maps, depending on the underlying hardware.  Physical
  170.      and non-paged maps are always valid in the process address
  171.      space; references won't cause a page fault.
  172.  
  173.      When _m_m_a_p increases a program's address space, it also
  174.      attempts to increase its allowable resident set size.
  175.  
  176. RREETTUURRNN VVAALLUUEE
  177.      _M_m_a_p returns the address of the mapped region when success-
  178.      ful.  (Note: this may change, as some Unix machines return
  179.      0.) Otherwise it returns -1 and places the error number in
  180.      the global variable _e_r_r_n_o.
  181.  
  182. EEXXAAMMPPLLEESS
  183.      The following code sets up a 1-Mbyte region of shared memory
  184.      at the first page boundary above the current program
  185.      ``break.'' This region will be shared with the process's
  186.      children and with any other process that maps the file
  187.      ``shmem''.  pgsz = getpagesize(); shm_base = (char *) (
  188.      ((int)sbrk(0) + (pgsz-1)) & ~(pgsz-1) ); fd = open ("shmem",
  189.      O_CREAT | O_RDWR, 0666); mmap (shm_base, 0x100000,
  190.      PROT_RDWR, MAP_SHARED, fd, 0);
  191.  
  192.  
  193.  
  194.  
  195. DYNIX                                                           3
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. MMAP                  C Library Procedures                   MMAP
  203.  
  204.  
  205.  
  206.      The following code maps the first page of Atomic Lock Memory
  207.      into the process's virtual address space at address
  208.      0x200000.  This region will be shared with the process's
  209.      children and with any other process that maps the file
  210.      ``/dev/alm/alm00''.  pgsz = getpagesize(); fd = open
  211.      ("/dev/alm/alm00", O_RDWR, 0); mmap (0x200000, pgsz,
  212.      PROT_RDWR, MAP_SHARED, fd, 0);
  213.  
  214. EERRRROORRSS
  215.      [EINVAL]       _A_d_d_r, _p_o_s, or _l_e_n is not a multiple of the
  216.                     system page size.
  217.  
  218.      [EINVAL]       _P_r_o_t did not specify at least one of
  219.                     PROT_WRITE or PROT_READ; _s_h_a_r_e did not
  220.                     specify MAP_SHARED, MAP_PRIVATE, or
  221.                     MAP_ZEROFILL; or _s_h_a_r_e specified MAP_ZEROFILL
  222.                     but _p_r_o_t did not contain PROT_RDWR.
  223.  
  224.      [EINVAL]       _F_d does not represent a regular or character
  225.                     special file.
  226.  
  227.      [EINVAL]       The process is the child of a _v_f_o_r_k.
  228.  
  229.      [EINVAL]       The area defined by the _a_d_d_r and _l_e_n argu-
  230.                     ments overlaps text or stack pages of the
  231.                     process.
  232.  
  233.      [ENODEV]       The device driver indicated by _f_d does not
  234.                     support mapping.
  235.  
  236.      [ENOMEM]       There is no swap space for the page table of
  237.                     a mapped regular file, or you are trying to
  238.                     create too large a process.
  239.  
  240.      [EMFILE]       The system-defined per-process limit on the
  241.                     number of _m_m_a_ped files (currently 8) was
  242.                     exceeded.
  243.  
  244.      [ENFILE]       The system-wide limit on the number of mapped
  245.                     regular files was exceeded.  This limit is
  246.                     defined by the variable _n_m_f_i_l_e in
  247.                     /_s_y_s/_c_o_n_f/_p_a_r_a_m._c.
  248.  
  249.      [EACCES]       _F_d does not allow the desired access (read or
  250.                     write), or a write-only file descriptor was
  251.                     used.
  252.  
  253.      [EACCES]       A mapped regular file must be extended to a
  254.                     file-system block boundary, or the file must
  255.                     have space allocated, and the file descriptor
  256.                     is read-only.
  257.  
  258.  
  259.  
  260.  
  261. DYNIX                                                           4
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. MMAP                  C Library Procedures                   MMAP
  269.  
  270.  
  271.  
  272.      [ENOSPC]       A mapped regular file was sparse and there
  273.                     was insufficient space in the file-system to
  274.                     satisfy the request.
  275.  
  276.      [EFBIG]        The _p_o_s and _l_e_n arguments would create too
  277.                     large a file.
  278.  
  279.      [others]       Other error values may be returned by some
  280.                     device drivers when requested to map.  See
  281.                     the relevant driver manual entry for details.
  282.  
  283. SSEEEE AALLSSOO
  284.      munmap(2), pmap(4), vm_ctl(2), fork(2), exec(2), get-
  285.      pagesize(2), msync(2), mlock(2), munlock(2), mincore(2)
  286.      _G_u_i_d_e _t_o _P_a_r_a_l_l_e_l _P_r_o_g_r_a_m_m_i_n_g
  287.  
  288. BBUUGGSS
  289.      A mapped file may not be truncated.
  290.  
  291.      If a file is extended to a file-system block boundary, its
  292.      original size is lost.
  293.  
  294.      Current restrictions on what parts of the address space can
  295.      be re-mapped should be lifted.
  296.  
  297. NNOOTTEESS
  298.      Due to a hardware restriction, PROT_WRITE implies PROT_READ
  299.      also.  PROT_EXEC is ignored.
  300.  
  301.      To minimize overhead, mapped regions should be kept as close
  302.      as possible to the low end of process memory.
  303.  
  304.      Address space holes under the process ``break'' read as
  305.      zeroes in core files.
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327. DYNIX                                                           5
  328.  
  329.  
  330.  
  331.